package
{
	import com.greensock.TweenLite;
	
	import events.MyEvent;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageDisplayState;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filters.DropShadowFilter;
	
	import view.InitView;
	import view.WorkView;

	public class PNG_CUTOUT extends Sprite
	{
		[Embed(source="128.png")]
		private var _pic_jpg:Class;
		private var pic_jpg:Bitmap;
		
		
		[Embed(source="asset/top_box.png")]
		private var _bg:Class;
		private var bg_bitmap:Bitmap;
		private var bg:Sprite;
		private var dropShadow:DropShadowFilter=new DropShadowFilter(5,90,0,0.5,8,8,0.7);
		private var max_width:Number;
		private var max_height:Number;
		private var state:uint=0;
		private var initView:InitView;
		private var workView:WorkView;
		private var handleDir:File;
		private var exportDir:File;
		public function PNG_CUTOUT()
		{
			this.stage.align=StageAlign.TOP_LEFT;
			this.stage.scaleMode=StageScaleMode.NO_SCALE;
			this.stage.color=0xeeeeee;
			this.stage.frameRate=30;
			stage.addEventListener(Event.RESIZE,handleResize);
			max_width=this.stage.stageWidth;
			max_height=this.stage.stageHeight;
			init();
		}
		
		private function handleResize(e:Event):void
		{
			max_width=this.stage.stageWidth;
			max_height=this.stage.stageHeight;
			
		}
		
		private function init():void
		{
			bg_bitmap=new _bg() as Bitmap;
			bg_bitmap.smoothing=true;
			
			bg=new Sprite();
			bg.addChild(bg_bitmap);
			bg.filters=[dropShadow];
			bg.x=15;
			bg.y=15;
			this.addChild(bg);
			pic_jpg=new _pic_jpg() as Bitmap;
			this.addChild(pic_jpg);
			pic_jpg.smoothing=true;
			pic_jpg.width=50;
			pic_jpg.height=50;
			pic_jpg.x=10;
			pic_jpg.y=10;
			changeState();
		}
		
		
		private function changeState():void
		{
			switch(state)
			{
				case 0:
					closeLastState(1);
					initView=new InitView();
					this.addChild(initView);
					initView.x=15;
					initView.y=100;
					initView.alpha=0;
					TweenLite.to(initView,1,{alpha:1,y:15});
					initView.addEventListener("folderChoseReady",handleFolderChoseReady);
					break;
				case 1:
					closeLastState(0);
					workView=new WorkView(handleDir,exportDir);
					this.addChild(workView);
					workView.x=15;
					workView.y=100;
					workView.alpha=0;
					workView.addEventListener("workOut",handleWorkout);
					TweenLite.to(workView,1,{alpha:1,y:15});
					break;
			}
			
		}
		
		private function handleFolderChoseReady(e:MyEvent):void
		{
			handleDir=e.obj[0];
			exportDir=e.obj[1];
			state=1;
			TweenLite.to(initView,0.5,{alpha:0,y:-200,onComplete:changeState});
		}
		
		private function handleWorkout(e:MyEvent):void
		{
			state=0;
			
			TweenLite.to(workView,0.5,{alpha:0,y:200,onComplete:changeState});
		}
		
		private function closeLastState(value:uint):void
		{
			switch(value)
			{
				case 0:
					this.removeChild(initView);
					initView.removeEventListener("folderChoseReady",handleFolderChoseReady);
					initView=null;
					break;
				case 1:
					if(workView)
					{
						this.removeChild(workView);
						workView.removeEventListener("workOut",handleWorkout);
						workView=null;
					}
					
					break;
			}
		}
	}
}